const { login, createUser,getUserInfo,updateUserInfo,updateUserAvatar } = require('../controller/userController')
const { Router } = require('express')

const fs = require('fs')
const path = require('path')
const multer  = require('multer')
const upload = multer({ dest: 'uploads/' })

let router = new Router()

//注册
router.post('/register', (req, res) => {
    let userInfo = req.body

    createUser(userInfo).then(
        value => {
            res.json({
                code:200,
                msg:value
            })
        },
        reason => {
            res.json({
                code:201,
                msg:reason
            })
        }
    )
})

//登录
router.post('/login', (req, res) => {
    let loginInfo = req.body

    login(loginInfo).then(
        value => {
            if(typeof(value) == 'object'){
                req.session._id = value._id.toString()

                res.json({
                    code:200,
                    msg:"登录成功"
                })
            }else{
                res.json({
                    code:201,
                    msg:value
                })
            }
        },
        reason => {
            res.json({
                code:200,
                msg:reason
            })
        }
    )
})

//退出登录
router.get('/logout',(req,res) => {
    req.session.user = null
    req.session.save(function (err) {
        if (err) console.log(err)

        req.session.regenerate(function (err) {
            if (err) console.log(err)
            res.json({
                code:200,
                msg:'退出成功'
            })
        })
    })
})

//获取用户个人信息
router.get('/user',(req,res)=>{
    const {_id} = req.session

    if(_id == undefined){
        res.json({
            code:201,
            msg:"获取失败"
        })
    }else{
        getUserInfo(_id).then(value=>{
            res.json({
                code:200,
                msg:'获取成功',
                data:value
            })
        },reason=>{
            res.json({
                code:201,
                msg:reason,
            })
        })
    }
})

//修改用户个人信息
router.put('/user',async(req,res)=>{
    try {
        await updateUserInfo(req.body)
        res.json({
            code:200,
            msg:'修改成功',
        })
    } catch (error) {
        console.log(error);
        res.json({
            code:201,
            msg:'修改失败'
        })
    }
})

//获取头像
router.get('/upload/avatar/:imageUrl',(req,res)=>{
    let {imageUrl} = req.params
    imageUrl = imageUrl.slice(0,28)
    let avatarPath = path.join('F:/VSCodeProject/Nodejs/genshinImpactServer/upload/avatar/'+ imageUrl)
    
    if(imageUrl){
        res.sendFile(avatarPath)
    }else{
        res.json({
            code:202,
            msg:'获取失败'
        })
    }
})

//用户上传头像
router.post('/upload/avatar', upload.single('avatar'), function (req, res, next) {
    // req.file 是 `avatar` 文件的信息
    // req.body 将具有文本域数据，如果存在的话
    const {_id} = req.body

    let newPath = path.join('./upload/avatar/' + _id + '.png')
    let imageUrl = 'http://127.0.0.1:3000/upload/avatar/' + _id + '.png'
    fs.rename(req.file.path,newPath,(err)=>{
        if(err){
            res.json({
                code:204,
                msg:'上传失败'
            })
        }else{
            updateUserAvatar(_id,imageUrl).then((value)=>{
                res.json({
                    code:200,
                    msg:'上传成功'
                })
            },(reason)=>{
                res.json({
                    code:204,
                    msg:'上传失败'
                })
            })
        }
    })
})

module.exports = function(){
    return router
}